package com.flower.controller.buyer;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flower.annotation.Log;
import com.flower.domain.dto.buyer.cart.CartDto;
import com.flower.domain.dto.buyer.cart.CartQueryDto;
import com.flower.domain.vo.buyer.CartVO;
import com.flower.service.CartService;
import com.flower.utils.AjaxResult;
import com.flower.utils.UserContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 购买者端购物车管理
 */
@Slf4j
@RestController
@RequestMapping("/buyer/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 分页获取购物车列表
     * @param queryDto
     * @return
     */
    @GetMapping("/list")
    public AjaxResult getCartList(CartQueryDto queryDto) {
        Long buyerId = UserContext.getUserId();
        queryDto.setBuyerId(buyerId.intValue());
        Page<CartVO> page = cartService.getCartList(queryDto);
        return AjaxResult.success(page);
    }

    /**
     * 添加到购物车
     * @param cartDto
     * @return
     */
    @Log(name = "添加到购物车")
    @PostMapping("/add")
    public AjaxResult addToCart(@RequestBody CartDto cartDto) {
        cartDto.setBuyerId(UserContext.getUserId().intValue());
        cartService.addToCart(cartDto);
        return AjaxResult.success();
    }

    /**
     * 更新购物车
     * @param cartDto
     * @return
     */
    @PutMapping("/update")
    public AjaxResult updateCart(@RequestBody CartDto cartDto) {
        cartDto.setBuyerId(UserContext.getUserId().intValue());
        cartService.updateCart(cartDto);
        return AjaxResult.success();
    }

    /**
     * 删除购物车商品
     * @param cartId
     * @return
     */
    @Log(name = "删除购物车商品")
    @DeleteMapping("/delete/{cartId}")
    public AjaxResult deleteCart(@PathVariable Integer cartId) {
        cartService.removeById(cartId);
        return AjaxResult.success();
    }

    /**
     * 清空购物车
     * @return
     */
    @Log(name = "清空购物车")
    @DeleteMapping("/clear")
    public AjaxResult clearCart() {
        Long buyerId = UserContext.getUserId();
        cartService.clearCart(buyerId);
        return AjaxResult.success();
    }
} 